package com.smart.campus.web.controller.school;

import java.util.List;

import com.smart.campus.framework.util.ShiroUtils;
import com.smart.campus.school.domain.ApproveList;
import com.smart.campus.school.domain.ClassInfo;
import com.smart.campus.school.domain.TeacherApplyList;
import com.smart.campus.school.service.IApproveListService;
import com.smart.campus.school.service.IClassInfoService;
import com.smart.campus.school.service.IGradeService;
import com.smart.campus.system.domain.SysRole;
import com.smart.campus.system.domain.SysUser;
import com.smart.campus.system.utils.DictUtils;
import com.smart.campus.web.utils.SchoolUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.school.domain.ApplyList;
import com.smart.campus.school.service.IApplyListService;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.utils.poi.ExcelUtil;
import com.smart.campus.common.core.page.TableDataInfo;

/**
 * 学生请假申请Controller
 * 
 * @author Zhihe.Li
 * @date 2020-06-15
 */
@Controller
@RequestMapping("/school/apply")
public class ApplyListController extends BaseController
{
    private String prefix = "school/apply";

    @Autowired
    private IApplyListService applyListService;

    @Autowired
    private IApproveListService approveListService;

    @Autowired
    private IClassInfoService classInfoService;

    @Autowired
    private IGradeService gradeService;

    @RequiresPermissions("school:apply:view")
    @GetMapping()
    public String list()
    {
        return prefix + "/apply";
    }

    /**
     * 查询学生请假申请列表
     */
    @RequiresPermissions("school:apply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ApplyList applyList)
    {
        startPage();
        //SysUser sysUser = ShiroUtils.getSysUser();
        applyList.setSchoolId(SchoolUtil.getSchoolId());
        applyList.setApproverIds(ShiroUtils.getUserId().toString());
        List<ApplyList> list = applyListService.selectApplyListList(applyList);
        return getDataTable(list);
    }


    private boolean isTeacher(SysUser sysUser){
        List<SysRole> roleList = sysUser.getRoles();
        for (SysRole sysRole : roleList) {
            if(sysRole.getRoleKey().equals("teacher")){
                return true;
            }
        }
        return false;
    }

    /**
     * 审批页面
     * @return
     */
    @RequiresPermissions("school:apply:approve")
    @GetMapping("/approve/{id}")
    public String approve(@PathVariable("id") Long id, ModelMap mmap)
    {
        ApplyList applyList = applyListService.selectApplyListById(id);

        mmap.put("applyList", applyList);
        return prefix + "/approve";
    }

    /**
     * 审批
     */
    @RequiresPermissions("school:apply:approve")
    @PostMapping("/approve")
    @ResponseBody
    public AjaxResult approve(ApproveList approveList)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        approveList.setApproveId(sysUser.getUserId());
        approveList.setApprover(sysUser.getUserName());
        ApplyList old = applyListService.selectApplyListById(approveList.getApplyId());
        int status = SchoolUtil.getApplySpStatus(approveList.getStatus(),old.getStatus());
        ApplyList applyList = new ApplyList();
        applyList.setStatus(status);
        applyList.setId(approveList.getApplyId());
        applyListService.updateApplyList(applyList);
        approveListService.insertApproveList(approveList);
        return AjaxResult.success();
    }
}
